package com.itboy.mall.wms.wareinfo.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import com.itboy.common.exception.CustomException;
import com.itboy.common.utils.PageUtils;
import com.itboy.common.core.page.PagePlus;
import com.itboy.common.core.page.TableDataInfo;
import org.springframework.stereotype.Service;
import com.itboy.common.core.mybatisplus.core.ServicePlusImpl;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.itboy.mall.wms.wareinfo.bo.MallWareInfoAddBo;
import com.itboy.mall.wms.wareinfo.bo.MallWareInfoQueryBo;
import com.itboy.mall.wms.wareinfo.bo.MallWareInfoEditBo;
import com.itboy.mall.wms.wareinfo.domain.MallWareInfo;
import com.itboy.mall.wms.wareinfo.mapper.MallWareInfoMapper;
import com.itboy.mall.wms.wareinfo.vo.MallWareInfoVo;
import com.itboy.mall.wms.wareinfo.service.IMallWareInfoService;

import java.util.List;
import java.util.Map;
import java.util.Collection;

/**
 * 仓库信息Service业务层处理
 *
 * @author itboy
 * @date 2021-08-10
 */
@Service
public class MallWareInfoServiceImpl extends ServicePlusImpl<MallWareInfoMapper, MallWareInfo> implements IMallWareInfoService {

    @Override
    public MallWareInfoVo queryById(Long id){
        return getVoById(id, MallWareInfoVo.class);
    }

    @Override
    public TableDataInfo<MallWareInfoVo> queryPageList(MallWareInfoQueryBo bo) {
        PagePlus<MallWareInfo, MallWareInfoVo> result = pageVo(PageUtils.buildPagePlus(), buildQueryWrapper(bo), MallWareInfoVo.class);
        return PageUtils.buildDataInfo(result);
    }

    @Override
    public List<MallWareInfoVo> queryList(MallWareInfoQueryBo bo) {
        return listVo(buildQueryWrapper(bo), MallWareInfoVo.class);
    }

    private LambdaQueryWrapper<MallWareInfo> buildQueryWrapper(MallWareInfoQueryBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<MallWareInfo> lqw = Wrappers.lambdaQuery();
        lqw.like(StrUtil.isNotBlank(bo.getName()), MallWareInfo::getName, bo.getName());
        lqw.like(StrUtil.isNotBlank(bo.getAddress()), MallWareInfo::getAddress, bo.getAddress());
        lqw.like(StrUtil.isNotBlank(bo.getAreaCode()), MallWareInfo::getAreaCode, bo.getAreaCode());
        return lqw;
    }

    @Override
    public Boolean insertByAddBo(MallWareInfoAddBo bo) {
        MallWareInfo add = BeanUtil.toBean(bo, MallWareInfo.class);
		LambdaQueryWrapper<MallWareInfo> queryWrapper = new LambdaQueryWrapper<>();
		queryWrapper.eq(MallWareInfo::getAreaCode,bo.getAreaCode());
		MallWareInfo mallWareInfo = this.baseMapper.selectOne(queryWrapper);
		if(ObjectUtil.isNotNull(mallWareInfo)){
			throw new CustomException("该区域编码已存在！");
		}
        return save(add);
    }

    @Override
    public Boolean updateByEditBo(MallWareInfoEditBo bo) {
        MallWareInfo update = BeanUtil.toBean(bo, MallWareInfo.class);
		LambdaQueryWrapper<MallWareInfo> queryWrapper = new LambdaQueryWrapper<>();
		queryWrapper.eq(MallWareInfo::getAreaCode,bo.getAreaCode());
		queryWrapper.ne(MallWareInfo::getId,bo.getId());
		List<MallWareInfo> mallWareInfoList = this.baseMapper.selectList(queryWrapper);
		if(CollectionUtil.isNotEmpty(mallWareInfoList)){
			throw new CustomException("该区域编码已存在！");
		}
		return updateById(update);
    }


    @Override
    public Boolean deleteWithValidByIds(Collection<Long> ids, Boolean isValid) {
        if(isValid){
            //TODO 做一些业务上的校验,判断是否需要校验
        }
        return removeByIds(ids);
    }
}
